//
//  Func_iOS.m
//  Demo
//
//  Created by migame on 15/12/14.
//
//

#include "ShowLayer.h"
#include "DownLoadCenter.h"
#include "Func_ios.h"
#include "ShowItem.h"


using namespace ui;




bool ShowLayer::init()
{
    if( Node::init()==false )return false;
    m_ShowItemAry = __Array::create();
    m_ShowItemAry->retain();
    for(int i=0;i<10;i++)
    {
        __Array *ary = __Array::create();
        m_ShowItemAry->addObject(ary);
    }
    // data
    M_dataAry = __Array::create();
    M_dataAry->retain();
    m_startShowTaskId = 0;
    updateIndex = 0;
    return true;
}





void ShowLayer::show()
{
    Director::getInstance()->getRunningScene()->addChild(this);
    //
    auto rootNode = CSLoader::createNode("csd/ShowLayer.csb");
    this->addChild(rootNode);
    //
    float wid = 250+20;
    float hei = 320+20;
    auto winsize = Director::getInstance()->getWinSize();
    m_mainSCView = (ScrollView*)rootNode->getChildByName("ScrollView_1");
    m_mainSCView->setContentSize(winsize);
    m_mainSCView->setInnerContainerSize( Size(wid*10, hei*10));
    //
    for(int i=0;i<10;i++)
    {
        __Array *lineAry = (__Array*)m_ShowItemAry->getObjectAtIndex(i);
        for(int y=0;y<10;y++)
        {
            ShowItem *si = ShowItem::create();
            m_mainSCView->addChild(si);
            si->setPosition( Point(wid*i+wid/2, hei*y+hei/2) );
            lineAry->addObject(si);
        }
    }
    // 第一次准备数据.
    reloadData();
    // 逐帧刷新更新好的图片和文字。
    this->scheduleUpdate();
}




void ShowLayer::onClick(Button *btn, int x, int y)
{
    log("点击 单个Item ");
}






void ShowLayer::reloadData()
{
    M_dataAry->removeAllObjects();
    DownLoadCenter::getInstance()->loadDataToArray(M_dataAry);
    log("已经能成功显示的数据数量: %ld", M_dataAry->count() );
}




void ShowLayer::refreshView()
{
}






void ShowLayer::update(float dt)
{
    updateIndex = updateIndex%100;
    int x = updateIndex%10;
    int y = 9 - updateIndex/10;
    ShowItem *item = (ShowItem *)((__Array*)m_ShowItemAry->getObjectAtIndex(x))->getObjectAtIndex(y);
    if(M_dataAry==nullptr) return;

    if( updateIndex>=M_dataAry->count() )
        item->setBindTaskId(-1);
    else{
        __Dictionary *oneInfo = (__Dictionary *)M_dataAry->getObjectAtIndex(updateIndex);
        int taskId = oneInfo->valueForKey(INFO_ONE_ID)->intValue();
        item->setBindTaskId(taskId);
        item->updateShow(oneInfo);
    }
    updateIndex ++;
}














